<?php
namespace app\zw_admin\controller;
use \think\Controller;
use think\Db;
// use think\Controller;
use think\Session;
use think\Request;
use think\File;
// use app\zw_admin\controller\ComController;
class SellingController extends ComController
{
    /**
     * 卖场信息
     * @return [type] [description]
     */
    public function index()
    {
        $list = Db::name('selling')->select();
            // dump($list);
            // exit;
        $this->assign('list',$list);
        return $this->fetch('index');
    }


    /**
     * 卖场信息添加
     */
    public function add()
    {

        if (Request::instance()->isPost()){
            //获取表单其他信息
            $list = input('post.');
            $brand_list = implode(",",$list['brand_name']);
            $brand_id = $list['brand_name'];
            $list['b_id'] = $brand_list;
            unset($list['brand_name']);
           
            foreach ($brand_id as $key => $value) {
                $er['brand_id'] = $value;
                $arr[] = $er;
            }

            // dump($arr);
            // exit;
            // dump(input('post.'));
            // 
            $selling_id = Db::name('selling')->insertGetId($list);
            if (!empty($selling_id)) {
                $num['number'] = $selling_id.'0000';
                Db::name('selling')->where(['selling_id'=>$selling_id])->update($num);
            }
            // echo $selling_id;
            // exit;
            foreach ($arr as $key => $value) {
                // $res = $value;
                // $res['selling_id'] = $selling_id;
                $value['selling_id'] = $selling_id;
                $res [] = $value;

            }
            // dump($res);
            // exit;

            if (db('selling_brand')->insertAll($res) > 0) {
                return $this->success('添加成功');
            }else{
                return $this->error('添加失败');
            }
           
        }else{

            $list = Db::name('brand')->select();
            // dump($list);
            // exit;
            $this->assign('list',$list);
            return $this->fetch('add');
        }
        
    }



    /**
     * 修改卖场信息
     * @return [type] [description]
     */
    public function edit()
    {
        // dump(input('param.'));
        $selling_id = input('param.selling_id');

        $selling_list = db('selling')->where(['selling_id'=>$selling_id])->select();
        $this->assign('selling_list',$selling_list);
        return $this->fetch('edit');
        // dump($selling_list);
    }


    public function edit_to()
    {
        $list = input('param.');
         $file = request()->file('img');
         $selling_id = $list['selling_id'];
         unset($list['selling_id']);
        // dump($file);
        // 移动到框架应用根目录/public/uploads/ 目录下
        if($file){
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
            if($info){
                //获取图片路径
                $list['selling_img'] =  'uploads/'.$info->getSaveName();
            }else{
                // 上传失败获取错误信息
                echo $file->getError();
            }
        }
        if (db('selling')->where(['selling_id'=>$selling_id])->update($list) >0) {
            return $this->success('添加成功');
        }else{
            return $this->error('添加失败');
        }


        // dump($list);
    }

}
